var gulp = require('gulp');
var less = require('gulp-less');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var fs = require("fs");
var proxy = require('http-proxy-middleware');
var fileinclude = require('gulp-file-include');

var path = {
  js: {
    src: "src/js/**",
    dist: "dist/js"
  },
  css: {
    src: "src/less/index.less",
    dist: "dist/css"
  },
  html: {

  }
}

var jsonProxy = proxy('/proxy', {
  target: 'http://lmapp.jd.com',
  changeOrigin: true,
  logLevel: 'debug'
})

gulp.task('server', ['less', 'js', 'html'], function () {
  browserSync.init({
    server: {
      baseDir: "./dist",
      middleware: [jsonProxy]
    },
  });
  gulp.watch("src/less/*.less", ['less']);
  gulp.watch(["src/js/**"], ['js']);
  gulp.watch(["src/*.html"], ['html']);
});

gulp.task('html', function () {
  gulp.src(['src/*.html'])
    .pipe(fileinclude({
      prefix: '@@',
      basepath: '@file'
    }))
    .pipe(gulp.dest('dist/'))
    .pipe(reload({ stream: true }))
});

gulp.task('js', function () {
  gulp.src(["src/js/**"])
    .pipe((gulp.dest('dist/js/')))
    .pipe(reload({ stream: true }))
});

gulp.task('less', function () {
  gulp.src(['src/less/index.less'])
    .pipe(less())
    .pipe(gulp.dest('./dist/css/'))
    .pipe(browserSync.stream());
});

gulp.task('default', ['server']);